<?php

get_template_part('secondary', 'header');
get_template_part('secondary', 'sidebar');

global $wp_query, $query_string;

$termname = $wp_query->queried_object->name;
$termslug = $wp_query->queried_object->slug;

$selected = ' selected="selected" ';
$main_link = Helpers::get_consumption_type_url($termslug);
$cp_link = Helpers::get_consumption_type_filtered_url($termslug, 'corto-plazo');
$mp_link = Helpers::get_consumption_type_filtered_url($termslug, 'mediano-plazo');
$lp_link = Helpers::get_consumption_type_filtered_url($termslug, 'largo-plazo');

$filter = $_GET['plazo'];

if ($query_string != "") {
    $query_string = $query_string . '&orderby=title&order=ASC&plazo=' . $filter;
    query_posts($query_string);
}

$paged = $wp_query->query_vars['paged'];
if ($paged == 0) {
    $paged = 1;
}

?>

<script type="text/javascript"><!--
    jQuery(document).ready(function($){
 
        $('#dropdown').change(function(e) {
            window.location.href = $("#dropdown option:selected").attr("data-link");
        });
  
    });
</script>

<div id="dash-content">
    <header class="header-buenas-practicas">
        <h2>BUENAS PR&Aacute;CTICAS AMBIENTALES</h2>
    </header> 
    <div class="blog-contetn">
        <?php if (have_posts()) : ?>
            <ul id="blog">
                <li>
                    <h3><?php echo $termname; ?></h3>
                    <h4>Se encontraron <?php echo $wp_query->found_posts; ?> buenas pr&aacute;cticas</h4>
                    <div class="blog-filter">
                        <select id="dropdown" style="border:1px">
                            <option value="all" data-link="<?php echo $main_link; ?>" <?php ?>>Todas las buenas prácticas de <?php echo ucwords($termname); ?></option>
                            <option value="cp"  data-link="<?php echo $cp_link; ?>" <?php
                                if ($filter == "corto-plazo") {
                                    echo $selected;
                                }
                                ?>>Corto Plazo</option>
                            <option value="mp"  data-link="<?php echo $mp_link; ?>" <?php
                                if ($filter == "mediano-plazo") {
                                    echo $selected;
                                }
                                ?>>Mediano Plazo</option>
                            <option value="lg"  data-link="<?php echo $lp_link; ?>" <?php
                                if ($filter == "largo-plazo") {
                                    echo $selected;
                                }
                                ?>>Largo Plazo</option>
                        </select>
                    </div>
                </li>
                <?php while (have_posts()) : the_post(); ?>
                    <li>
                        <a href="<?php the_permalink(); ?>">
                            <figure>
                                <?php the_post_thumbnail(array(248, 145)); ?>
                            </figure>
                            <h3><?php the_title(); ?></h3>
                            <p><?php the_excerpt(); ?></p>
                        </a>
                    </li>
                <?php endwhile; ?>
            </ul>
        <?php endif; ?>
        <div id="pagination">
            <div class="pag-prev">
                <?php previous_posts_link('Anterior'); ?>
            </div>
            <div class="pags">
                <ul class="paginas">
                    <?php for ($i = 1; $i <= $wp_query->max_num_pages; $i++) { 
                            $pagelink = "";
                            if(isset($filter)){
                                $pagelink = Helpers::get_consumption_type_paginated_filtered_url($termslug, $i, $filter);
                            }else {
                                $pagelink = Helpers::get_consumption_type_paginated_url($termslug, $i);
                            }
                        ?>
                            <li>
                                <a href="<?php echo $pagelink ?>" <?php
                                    if ($i == $paged) {
                                        echo 'class="bolded"';
                                    }
                                    ?> ><?php echo $i; ?></a>
                            </li>
                    <?php } ?>
                </ul>
            </div>
            <div class="pag-next">
                <?php next_posts_link('Siguiente'); ?>
            </div>
        </div>
    </div>

</div>
